package com.nzy.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nzy.controller.request.ListOrdersRequest;
import com.nzy.controller.response.ListOrdersResponse;
import com.nzy.dao.mapper.OrdersMapper;
import com.nzy.entity.Orders;
import com.nzy.mq.producer.MessageProducer;
import com.nzy.mq.producer.OrderMessage;
import com.nzy.service.OrdersService;
import com.nzy.service.StockFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private StockFeignService stockFeignService;

    @Autowired
    private MessageProducer messageProducer;



    @Override
    @Transactional
    public void submit(Orders orders) {
        Date date = new Date();
        orders.setCreateTime(date);
        orders.setUpdateTime(date);
        orders.setState(0);
        ordersMapper.insert(orders);
        sendStockMq(orders);
    }

    @Override
    public List<ListOrdersResponse> quesyList(ListOrdersRequest listOrdersRequest) {
        Page page = PageHelper.startPage(listOrdersRequest.getPageNum(), listOrdersRequest.getPageSize());
        Orders orders = new Orders();
        BeanUtils.copyProperties(listOrdersRequest, orders);
        List<Orders> list = ordersMapper.selectBySelect(orders);
        List<ListOrdersResponse> result = new ArrayList<>();
        for(Orders o: list){
            ListOrdersResponse response = new ListOrdersResponse();
            BeanUtils.copyProperties(o, response);
            response.setStock(getStock(response.getSkuId()));
            result.add(response);
        }
        return result;
    }

    public Integer getStock(String skuId){
        Integer result = 0;
        try{
            String response = stockFeignService.getStock(skuId);
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (jsonObject.getIntValue("code") == 200){
                result = jsonObject.getIntValue("data");
            }
        }catch (Exception e){

        }
        return  result;
    }

    public void sendStockMq(Orders orders){
        String id = UUID.randomUUID().toString();
        OrderMessage orderMessage = new OrderMessage();
        orderMessage.setOrderId(orders.getId());
        orderMessage.setSkuId(orders.getSkuId());
        orderMessage.setNumber(orders.getNumber());
        orderMessage.setUserId(orders.getUserId());
        orderMessage.setMsgId(UUID.randomUUID().toString().replaceAll("-", ""));
        log.info("发送扣库存消息：" + JSON.toJSONString(orderMessage));
        messageProducer.send(orderMessage);
    }
}
